package com.wwp.tbshop.view.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.LimitBuyBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.view.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

/**
 * Created by ${wenping} on 6/17/2017.
 */

public class SubjectGoodsAdapter extends RecyclerView.Adapter {

    private HomeFragment mHomeFragment;

    public SubjectGoodsAdapter(HomeFragment homeFragment) {
        mHomeFragment = homeFragment;
    }

    private List<LimitBuyBean.ProductListBean> subjectList = new ArrayList<>();

    public void setSubjectList(List<LimitBuyBean.ProductListBean> subjectList) {
        this.subjectList = subjectList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View firstView = View.inflate(UiUtil.getContext(), R.layout.item_home_first_type, null);
        FirstViewHolder firstViewHolder = new FirstViewHolder(firstView);
        return firstViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FirstViewHolder firstHolder = (FirstViewHolder) holder;
        firstHolder.setData(subjectList.get(position));
    }

    @Override
    public int getItemCount() {
        if (subjectList == null) {
            return 0;
        }
        return subjectList.size();
    }

    class FirstViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_first_type_iv)
        ImageView mItemFirstTypeIv;
        @Bind(R.id.item_first_type_name)
        TextView mItemFirstTypeName;
        @Bind(R.id.item_first_type_marketprice)
        TextView mItemFirstTypeMarketprice;
        @Bind(R.id.item_first_type_price)
        TextView mItemFirstTypePrice;

        FirstViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(LimitBuyBean.ProductListBean bean) {
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST+bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemFirstTypeIv);
            mItemFirstTypeName.setTextColor(Color.BLACK);
            mItemFirstTypeMarketprice.setTextColor(Color.BLACK);

            mItemFirstTypePrice.setTextColor(Color.BLACK);
            mItemFirstTypeName.setText(bean.getName()+"");
            mItemFirstTypeMarketprice.setText("市价:¥"+bean.getPrice()+"");
            mItemFirstTypePrice.setText("现价:¥"+bean.getLimitPrice()+"");
            Log.e(TAG, bean.getName()+bean.getPrice()+";"+bean.getLimitPrice());
        }
    }
}
